// Nur zum testen

#include "ServerSocket.h"
#include "Server.h"
#include <boost\thread.hpp>
#include <string>
#include <iostream>
#include <boost\asio.hpp>
#include <boost\bind.hpp>
using boost::asio::ip::tcp;
using namespace std;
using namespace boost::asio;



Server server("mail.example.com");

void printMenu()
{
	std::cout << "Server Beenden: exit" << endl;
	std::cout << "Dieses Menue anzeigen: help" << endl;
	//cout << "Benutzer hinzufuegen: add Username Password" << endl;
	//cout << "Benutzer loeschen: delete Username" << end;
}

void startServer()
{
	server.run();

}

void testSMTP()
{
	string localhost = "127.0.0.1";

	const int SZ = 128;
	char buf[SZ+1];
	size_t countBytes;
	io_service io_service;
	ip::tcp::endpoint serverEnd(ip::address::from_string(localhost), 25);
	ip::tcp::socket socket(io_service);
	socket.connect(serverEnd);
	//boost::system::error_code ignored_error;
	string dummy = "telnet mail.example.com 25";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << "Antwort vom Server: " << buf << endl;

	dummy = "HELO foobar.example.net";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << "Antwort vom Server: " << buf << endl;

	dummy = "MAIL FROM:<info@example.org>";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	dummy = "RCPT TO:<info@example.com>";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;
	
	dummy = "DATA";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	dummy = "From: <info@example.org>\nTo: <info@example.com>\nSubject: Testmail\nDate: Thu, 26 Oct 2006 13:10:50 +0200\n\nLorem ipsum dolor sit amet, \nconsectetur adipisici elit, sed \neiusmod tempor incidunt ut labore et \ndolore magna aliqua.\n.";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;
	
	dummy = "QUIT";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	socket.close();
}

void testSMTP2()
{
	string localhost = "127.0.0.1";

	const int SZ = 128;
	char buf[SZ+1];
	size_t countBytes;
	io_service io_service;
	ip::tcp::endpoint serverEnd(ip::address::from_string(localhost), 25);
	ip::tcp::socket socket(io_service);
	socket.connect(serverEnd);
	//boost::system::error_code ignored_error;
	string dummy = "telnet mail.example.com 25";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	dummy = "HELO foobar.example.net";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << "Antwort vom Server: " << buf << endl;

	dummy = "MAIL FROM:<info@example.com>";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	dummy = "RCPT TO:<info@example.org>";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;
	
	dummy = "DATA";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	dummy = "From: <info@example.com>\nTo: <info@example.org>\nSubject: Testmail\nDate: Thu, 26 Oct 2006 13:10:50 +0200\n\nLorem ipsum dolor sit amet, \nconsectetur adipisici elit, sed \neiusmod tempor incidunt ut labore et \ndolore magna aliqua.\n.";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;
	
	dummy = "QUIT";
	socket.write_some(boost::asio::buffer(dummy,dummy.length()));
	countBytes = socket.read_some(asio::buffer(buf, SZ));
	buf[countBytes] = '\0';
	cout << buf << endl;

	socket.close();
}


int main()
{
	bool run = true;
	string input = "";
	/*
	cout << "Bitte Servernamen eingeben" << endl;
	cin >> input;
	Server* server = new Server(input);
	*/
	boost::thread t3(startServer);

	
	cout << "Socket gestartet" << endl;
	printMenu();
	input = "";
	while(run)
	{
		cin >> input;
		if(input == "exit")
		{
			run = false;
		}else if(input == "help")
		{
			printMenu();
		}else if(input == "t")
		{
			testSMTP();
		}else if(input == "t2")
		{
			testSMTP2();
		}
	}
	cout << "Socket wird beendet" << endl;
	
	server.stop();
	t3.join();
	cout << "ready" << endl;
	//test only
	/*
	run = true;
	while(run)
	{
		cin >> input;
		if(input == "exit")
		{
			run = false;
		}
	}
	*/
	cin.get();
	return 0;
}